package org.timeismoney.payment.web.action.teacher;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.timeismoney.payment.common.Md5Util;
import org.timeismoney.payment.common.PageBean;
import org.timeismoney.payment.model.Student;
import org.timeismoney.payment.model.Unit;
import org.timeismoney.payment.service.StudentService;
import org.timeismoney.payment.service.UnitService;
import org.timeismoney.payment.web.WebUtils;
import org.timeismoney.payment.web.action.BaseAction;
import org.timeismoney.payment.web.form.StudentForm;

public class AdminStuAction extends BaseAction {
	private static Logger	log	= Logger.getLogger(AdminUnitAction.class);
	private StudentService	service;
	private UnitService		unitService;

	public void setService(StudentService service) {
		this.service = service;
	}

	public void setUnitService(UnitService unitService) {
		this.unitService = unitService;
	}

	@Override
	protected ActionForward unspecified(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		try {
			// rows=10&page=1
			// sidx schoolYearNo
			// sord desc
			String rows = request.getParameter("rows");
			String page = request.getParameter("page");
			String sortCol = request.getParameter("sidx");
			String sortD = request.getParameter("sord");
			int rowsNo = 10;
			try {
				rowsNo = Integer.valueOf(rows);
			} catch (RuntimeException e) {
			}
			int pageNo = 1;
			try {
				pageNo = Integer.valueOf(page);
			} catch (RuntimeException e) {
			}
			StudentForm studentForm = (StudentForm) form;
			PageBean<Student> pageBean = new PageBean<Student>(pageNo, rowsNo,
					sortCol, sortD);
			service.findStuByUnit(studentForm.getUnitNo(),
					studentForm.getSchoolYearNo(), pageBean);
			renderJson(response, WebUtils.generateJSONByPageBean(pageBean));
		} catch (Exception e) {
			log.error(e.getMessage());
			renderJson(
					response,
					WebUtils.generateErrorJSON(TEXT_ERROR_MSG_QUERY + " "
							+ e.getMessage()));
		}
		return null;
	}
	
	public ActionForward get(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		try {
			StudentForm studentForm = (StudentForm) form;
			Student student = service.get(studentForm.getStuNo());
			List<Student> stuList = new ArrayList<Student>();
			if(student!=null)
				stuList.add(student);
			PageBean<Student> pageBean = new PageBean<Student>(1,stuList.size(),10,stuList);
			renderJson(response, WebUtils.generateJSONByPageBean(pageBean));
		} catch (Exception e) {
			log.error(e.getMessage());
			renderJson(
					response,
					WebUtils.generateErrorJSON(TEXT_ERROR_MSG_QUERY + " "
							+ e.getMessage()));
		}
		return null;
	}
	
	public ActionForward add(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		try {
			StudentForm studentForm = (StudentForm) form;
			Unit unit = unitService.get(studentForm.getUnitNo());
			Student stu = new Student();
			stu.setUnit(unit);
			stu.setStuNo(studentForm.getStuNo());
			stu.setStuName(studentForm.getStuName());
			service.save(stu);
		} catch (Exception e) {
			log.error(e.getMessage());
			renderJson(
					response,
					WebUtils.generateErrorJSON(TEXT_ERROR_MSG_ADD + " "
							+ e.getMessage()));
		}
		return null;
	}

	public ActionForward edit(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		try {
			StudentForm studentForm = (StudentForm) form;
			Student stu = service.get(studentForm.getStuNo());
			if (stu.getUnit() != null
					&& !stu.getUnit().getUnitNo()
							.equals(studentForm.getUnitNo())) {
				Unit unit = unitService.get(studentForm.getUnitNo());
				stu.setUnit(unit);
			}
			stu.setStuName(studentForm.getStuName());
			service.update(stu);
		} catch (Exception e) {
			log.error(e.getMessage());
			renderJson(
					response,
					WebUtils.generateErrorJSON(TEXT_ERROR_MSG_EDIT + " "
							+ e.getMessage()));
		}
		return null;
	}

	public ActionForward del(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		try {
			StudentForm studentForm = (StudentForm) form;
			Student stu = service.get(studentForm.getStuNo());
			service.delete(stu);
		} catch (Exception e) {
			log.error(e.getMessage());
			renderJson(
					response,
					WebUtils.generateErrorJSON(TEXT_ERROR_MSG_DEL + " "
							+ e.getMessage()));
		}
		return null;
	}
	
	public ActionForward pwd(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		try {
			StudentForm studentForm = (StudentForm) form;
			Student stu = service.get(studentForm.getStuNo());
//			if(!stu.getPwd().equals(studentForm.getOrigPwd())){
//				throw new BizException("原始密码错误");
//			}
			stu.setPwd(Md5Util.MD5(studentForm.getPwd()));
			service.update(stu);
		} catch (Exception e) {
			log.error(e.getMessage());
			renderJson(
					response,
					WebUtils.generateErrorJSON(TEXT_ERROR_MSG_EDIT + " "
							+ e.getMessage()));
		}
		return null;
	}
}
